lib/repo: open file only if required
authorLuca BRUNO <luca.bruno@coreos.com>
Mon, 24 Jan 2022 16:46:40 +0000 (16:46 +0000)
committerLuca BRUNO <luca.bruno@coreos.com>
Mon, 24 Jan 2022 16:54:54 +0000 (16:54 +0000)
This tightens up the logic for opening a file while inspecting its
xattrs. The only codepath fetching xattrs from a FD is the one
handling 'bare' mode.
It also rearranges the else-assert flow, mostly for future-proofing.

src/libostree/ostree-repo.c

index 86948ee261b063f2ff71bc5c39067840f42f6e5a..64a8fea92d177417c3cee8674819217495c25366 100644 (file)
@@ -4218,8 +4218,9 @@ _ostree_repo_load_file_bare (OstreeRepo         *self,
                                           cancellable, error);
     }
 
-  const gboolean need_open =
-    (out_fd || out_xattrs || self->mode == OSTREE_REPO_MODE_BARE_USER);
+  const gboolean need_open = (out_fd ||
+                              (out_xattrs && self->mode == OSTREE_REPO_MODE_BARE) ||
+                              self->mode == OSTREE_REPO_MODE_BARE_USER);
   /* If it's a regular file and we're requested to return the fd, do it now. As
    * a special case in bare-user, we always do an open, since the stat() metadata
    * lives there.
@@ -4284,10 +4285,8 @@ _ostree_repo_load_file_bare (OstreeRepo         *self,
           ret_xattrs = g_variant_ref_sink (g_variant_builder_end (&builder));
         }
     }
-  else
+  else if (self->mode == OSTREE_REPO_MODE_BARE)
     {
-      g_assert (self->mode == OSTREE_REPO_MODE_BARE);
-
       if (S_ISREG (stbuf.st_mode) && out_xattrs)
         {
           if (self->disable_xattrs)
@@ -4306,6 +4305,10 @@ _ostree_repo_load_file_bare (OstreeRepo         *self,
             return FALSE;
         }
     }
+  else
+    {
+      g_assert_not_reached ();
+    }
 
   if (out_fd)
     *out_fd = glnx_steal_fd (&fd);